﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class admin_adminEdit : System.Web.UI.Page
{
    private String _oldPwd;

    public String oldPwd
    {
        get { return _oldPwd; }
        set { _oldPwd = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            getInfo();
        }
    }
    //获取信息
    public void getInfo() {
        OleDbConnection conn = DB.conn();
        OleDbCommand comm = null;
        string adminId = "",roleId="";
        if (Request["adminId"] != null) {
            adminId = Request["adminId"].ToString().Trim();
            roleId = AdminDao.getRoleId(adminId).ToString();
        }
        if (conn != null) {
            try
            {
                conn.Open();
                //获取角色
                comm = new OleDbCommand();
                comm.Connection = conn;
                string sql = "select * from roles ";
                //qiang为超管，显示所有
                if (Session["admin"] == null || !Session["admin"].ToString().Equals("qiang"))
                {
                    sql += " where roleName<>'qiang' ";
                }
                sql += " order by roleOrder";
                comm.CommandText = sql;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = comm;
                DataTable dt = new DataTable();
                da.Fill(dt);
                ListItem item = new ListItem();
                item.Text = "请选择";
                item.Value = "";
                roleDrop.Items.Insert(0, item);
                if (dt != null && dt.Rows.Count > 0) {
                    for (int i = 0; i < dt.Rows.Count; i++) {
                        item = new ListItem();
                        item.Text = dt.Rows[i]["roleName"].ToString() ;
                        item.Value = dt.Rows[i]["id"].ToString();
                        roleDrop.Items.Insert(i+1, item);
                        //判断是否为当前用户的角色
                        if (adminId.Length>0 && roleId.Equals(dt.Rows[i]["id"].ToString()))
                        {
                            roleDrop.SelectedIndex = i + 1;
                        }
                    }
                }

                //获取人员属性
                if (adminId.Length > 0)
                {
                    comm.Dispose();
                    comm.Connection = conn;
                    comm.CommandText = "select * from admin where id=" + adminId;
                    OleDbDataReader reader = comm.ExecuteReader();
                    if (reader != null && reader.HasRows) {
                        reader.Read();
                        userName.Text = reader.GetString(1);
                        oldName.Value = reader.GetString(1);
                        _oldPwd = reader.GetString(2);
                        userPass.Enabled = false;
                        if (reader.GetInt32(4)==1) {
                            assDrop.SelectedIndex = 1;
                        }
                    }
                }
            }
            catch (Exception error)
            {
                Response.Write(error.ToString());
            }
            finally {
                if (comm != null) {
                    comm.Dispose();
                }
                conn.Close();
            }
        }
    }
    //提交处理
    public void edit(object sender, EventArgs e)
    {
        string roleDrop = Request["roleDrop"].ToString();
        string userName = Request["userName"].ToString();
        string userPass = Request["userPass"].ToString();
        string assDrop = Request["assDrop"].ToString();
        try
        {
            if (Request["adminId"] != null && Request["adminId"].ToString().Trim().Length > 0)
            {
                if (Request["modPass"] != null && "1".Equals(Request["modPass"].ToString()))
                {
                    userPass = MD5.String2MD5(userPass);
                }
                //更新
                AdminDao.update(Request["adminId"].ToString().Trim(), roleDrop, userName, userPass, assDrop);
            }
            else
            {
                //添加
                AdminDao.add(roleDrop, userName, userPass, assDrop);
            }
            Response.Write("<script>alert('提交成功！');location.href='admin.aspx';</script>");
        }
        catch (Exception error)
        {
            Response.Write(error.ToString());
        }
    }
}
